﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using dhDataAcess.baseBLL;
using dhDataAcess.orderLinqDAL;
using dhDataAcess.dataAcess.orderDataSetTableAdapters;
using dhDataAcess.dataAcess;
using System.Data.SqlClient;
using dhDataAcess.orderDAL;

namespace dhSystemBLL.orderData
{
   public class orderDetailBLL
    {
       private orderDetailTableAdapter _orderDetailAdapter = null;
       private orderDetailTableAdapter orderDetailAdapter
        {
            get
            {
                if (_orderDetailAdapter == null) _orderDetailAdapter = new orderDetailTableAdapter();
                return _orderDetailAdapter;

            }
        }
        /// <summary>
       /// 查询订单明细表
        /// </summary>
        /// <returns></returns>

       public orderDataSet.orderDetailDataTable getorderDetailAll()
        {
            try
            {
                return orderDetailAdapter.GetDataorderDetailALL();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
       /// <summary>
       /// 根据订单号查询订单明细表
       /// </summary>
       /// <returns></returns>

       public orderDataSet.orderDetailDataTable getorderDetailByorderNo(string orderNo)
       {
           try
           {
               return orderDetailAdapter.GetDataorderDetailByorderNo(orderNo );
           }
           catch (Exception ex)
           {
               throw new Exception(ex.Message.ToString());
           }
       }
       /// <summary>
       /// 根据产品类型查询订单明细表
       /// </summary>
       /// <returns></returns>

       public orderDataSet.orderDetailDataTable getorderDetailByorderptype(string orderNo,string ptype)
       {
           try
           {
               return orderDetailAdapter.GetDataorderDetailByptype  (orderNo,ptype);
           }
           catch (Exception ex)
           {
               throw new Exception(ex.Message.ToString());
           }
       }
        /// <summary>
       /// 根据漂染单号查询订单明细表
        /// </summary>
        /// <param name="groupNum"></param>
        /// <returns></returns>
       public orderDataSet.orderDetailDataTable getorderDetailAll(string orderDetailNo)
       {
           try
           {
               return orderDetailAdapter.GetDataorderDetailByorderDetailNo(orderDetailNo);
           }
           catch (Exception ex)
           {
               throw new Exception(ex.Message.ToString());
           }
       }
             /// <summary>
       /// 根据漂染单号查询订单明细表
        /// </summary>
        /// <param name="groupNum"></param>
        /// <returns></returns>
       public orderDataSet.orderDetailDataTable getorderDetailAllByorderDetailNo(string orderDetailNo)
        {
            try
            {
                return orderDetailAdapter.GetDataorderDetailByorderDetailNo (orderDetailNo);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
            /// <summary>
            /// 插入订单明细表
            /// </summary>
            /// <param name="row"></param>
            /// <returns></returns>

        }
       /// <summary>
       /// 根据订单状态查询订单明细表
       /// </summary>
       /// <param name="groupNum"></param>
       /// <returns></returns>
       public orderDataSet.orderDetailDataTable getorderDetailAllBydetailStatus(string detailStatus)
       {
           try
           {
               return orderDetailAdapter.GetDataorderDetailBydetailStatus(detailStatus);
           }
           catch (Exception ex)
           {
               throw new Exception(ex.Message.ToString());
           }

           /// <summary>
           /// 插入订单明细表
           /// </summary>
           /// <param name="row"></param>
           /// <returns></returns>

       }
       public int orderDetailInsert(orderDetailModel row)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;

            try
            {
                tn = conn.BeginTransaction();
                orderDetailDAL orderDetail = new orderDetailDAL(tn, conn);
                orderDetail.orderDetailInsert(row);
                tn.Commit();
                return i;
            }
            catch (SqlException ex)
            {
                tn.Rollback();
                if (ex.Number == 2627)
                {
                    throw new Exception("这个客户编号已经存在");
                }


                throw new Exception(ex.Number.ToString() + ex.Message.ToString());

            }

            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());

            }
            finally
            {
                conn.Close();
            }
        }
        /// <summary>
       /// 更新订单明细表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
       public int orderDetailUpdate(orderDetailModel row)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                orderDetailDAL orderDetail = new orderDetailDAL(tn, conn);
                orderDetail.orderDetailUpdate (row);
                tn.Commit();
                return i;
            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());

            }
            finally
            {
                conn.Close();
            }
        }
        /// <summary>
        /// 删除一行订单明细
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        public int  orderDetailDel(string orderDetailNo)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
               orderDetailDAL orderDetail = new orderDetailDAL(tn, conn);
               orderDetail.orderDetailDel(orderDetailNo);
                tn.Commit();
                return i;
            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());

            }
            finally
            {
                conn.Close();
            }
        }
    }
}
